let or = new Vue({
    el: '#myOrder',
    data: {
        user: JSON.parse(sessionStorage.getItem("user")),
        orders: [],
        currentId: null,
        index: null
    },
    created() {

        let formData = {
            userId: this.user.userId
        }
        $.get('http://localhost:8080/cinema/api/SelectOrdersByUserIdServlet.do', formData, function (dto) {
            or.orders = dto.value;
        });
    },
    methods: {
        delOrder: function (orderid, i) {
            $(".hide-prompt").show();
            this.currentId = orderid;
            this.index = i;

        },
        yes: function () {
            let formData = {
                orderId: this.currentId
            }
            // 删除订单前查询该订单的付款状态 ，根据付款状态判断是否需要改变其座位状态
            $.get('http://localhost:8080/cinema/api/SelectOrderByIdServlet.do', formData, function (dto) {
                // 如果付款状态为1，则表示已付款 ，无需改变座位状态
                if (dto.value.status == 1) {
                    $.ajax({
                        url: 'http://localhost:8080/cinema/api/DeleteOrderByIdServlet.do',
                        type: 'delete',
                        data: JSON.stringify(formData),
                        dataType: 'json',
                        success: function (dto) {
                            if (dto.code == 200) {
                                or.orders.splice(or.index, 1);
                                $(".hide-prompt").hide();
                                // layer.msg(dto.value,{icon:1});
                            } else {
                                layer.msg(dto.msg, { icon: 5 });
                            }
                        }
    
                    });
                    return;
                }

                // 如果付款状态不为1，则表示未付款 ，需要改变座位状态
                // 发送异步，实现根据订单ID 删除订单
                $.ajax({
                    url: 'http://localhost:8080/cinema/api/DeleteOrderByIdServlet.do',
                    type: 'delete',
                    data: JSON.stringify(formData),
                    dataType: 'json',
                    success: function (dto) {
                        if (dto.code == 200) {

                            let currentSeat = JSON.parse(localStorage.getItem("currentSeat"));
                            // 从本地存储取出数据 ，根据循环遍历的座位ID、以及放映ID，来修改座位状态
                            for (let sid of currentSeat.seatIdarr) {
                                let formData2 = {
                                    seatId: sid,
                                    showId: currentSeat.showId,
                                    status: 0
                                }

                                // 发送异步，实现根据放映ID、座位ID 修改放映场次对应影厅的座位状态
                                $.ajax({
                                    url: 'http://localhost:8080/cinema/api/UpdateSeatStatusByIdServlet.do',
                                    type: 'put',
                                    data: JSON.stringify(formData2),
                                    dataType: 'json',
                                    success: function (dto) {

                                    }

                                });
                            }

                            or.orders.splice(or.index, 1);
                            $(".hide-prompt").hide();
                            // layer.msg(dto.value,{icon:1});
                        } else {
                            layer.msg(dto.msg, { icon: 5 });
                        }
                    }

                });

            }, "json");

        },
        no: function () {
            $(".hide-prompt").hide();
        },
        pay: function (id) {
            localStorage.setItem("orderId", id);
            location.href = "home_pay.html";
        },
        look: function (o) {
            let formData = {
                orderId: o.orderId,
                filmName: o.filmName,
                hallName: o.hallName,
                showDate: o.showDate,
                startTime: o.startTime,
                price: o.price,
                seats: o.seats
            }

            sessionStorage.setItem("orderInfo", JSON.stringify(formData));
            if (o.status == 1) {
                location.href = "home_done.html";
            } else {
                location.href = "home_waitPay.html";
            }
        },
        filmInfo:function(filmId){
            localStorage.setItem("filmId", filmId);
            
            window.open("home_filmInfo.html");
        },
        goSeatChoose:function(showId){
            localStorage.setItem("showId",showId);
            location.href="home_seatChoose.html";
        }
    }

});